class SettingOverridesController < ApplicationController

  # TODO rename all these actions so they are not prefixed with popup_ which is pointless

  def popup_new
    @bucket = Bucket.find(params[:bucket_id])
    @setting_override = SettingOverride.new()
    @setting = Setting.find(params[:setting_id])
    @axes = Axis.prioritized
		render :layout => false
  end

  def popup_create
    @bucket = Bucket.find(params[:bucket_id])
    @setting = Setting.find(params[:setting_id])
    @setting_override = SettingOverride.new(params[:setting_override])
    if @setting_override.save
      render :partial => 'buckets/settings_list', :locals => { :settings => @bucket.settings }
    else
      render :text => @setting_override.errors.full_messages.join(","), :status => 400
    end
  end

  def popup_edit
    @bucket = Bucket.find(params[:bucket_id])
    @axes = Axis.prioritized
    @setting_override = SettingOverride.find(params[:id])
    @setting = @setting_override.setting
		render :layout => false
  end

  def popup_update
    @bucket = Bucket.find(params[:bucket_id])
    @axes = Axis.prioritized
    @setting_override = SettingOverride.find(params[:id])
    @setting = @setting_override.setting
    if @setting_override.update_attributes(params[:setting_override])
      render :partial => 'buckets/settings_list', :locals => { :settings => @bucket.settings }
    else
      render :text => @setting_override.errors.full_messages.join(","), :status => 400
    end
  end

  def destroy
    SettingOverride.delete(params[:id])
    flash[:notice] = "Setting override deleted successfully"
    redirect_to bucket_url(Bucket.find(params[:bucket_id]))
  end

end
